package slw.display
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	import slw.core.UISprite;
	import slw.utils.Base64;
	import slw.utils.BitmapDataEncoder;
	
	
	/**
	 * Screencasts
	 * 显示对象截图类
	 * @author ddx<br/>
	 * 2011-6-30
	 *
	 */
	public class Screencasts extends UISprite
	{
		//
		public var bounds:Rectangle;
		
		private var _target:DisplayObject;
		
		private var _fps:Number=10;
		
		private var _simulateMovieClip:SimulateMovieClip;
		
		private var _datas:Array=[];
		
		private var _recordTimer:Timer;
		
		/**
		 * 构造函数
		 * 
		 */
		public function Screencasts(target:DisplayObject=null,bounds:Rectangle=null,fps:Number=10)
		{
			super();
			
			init();
			
			this.target=target;
			this.bounds=bounds;
			this.fps=fps;
		}
		
		/*
		-----------------------------------
		setters getters
		-----------------------------------
		*/
		//
		public function get simulateMovieClip():SimulateMovieClip{
			return _simulateMovieClip;
		}
		//
		public function get datas():Array{
			return _datas;
		}
		//
		public function set fps(value:Number):void{
			_fps=value;
			if(_fps<=0)
				_fps=10;
			
			_recordTimer.delay=1000/_fps;
		}
		//
		public function get fps():Number{
			return _fps;
		}
		//
		public function set target(value:DisplayObject):void{
			_target=value;
			
			if(_target!=null&&bounds==null){
				bounds=new Rectangle(0,0,_target.width,_target.height);
			}
		}
		//
		public function get target():DisplayObject{
			return _target;
		}
		/*
		-----------------------------------
		public  methods
		-----------------------------------
		*/
		//
		public function record():void{
			if(_target==null&&this.stage!=null){
				this.target=this.stage;
			}else if(_target==null){
				throw new Error("无录像目标.");
			}
			
			startTimer();
		}
		//
		public function stop():void{
			stopTimer();
		}
		/*
		-----------------------------------
		private  methods
		-----------------------------------
		*/
		//
		private function init():void{
			_simulateMovieClip=new SimulateMovieClip();
			this.addChild(_simulateMovieClip);
			
			_recordTimer=new Timer(1000/_fps);
		}
		//
		private function startTimer():void{
			_recordTimer.addEventListener(TimerEvent.TIMER,recordTimerTimer);
			_recordTimer.start();
		}
		//
		private function stopTimer():void{
			_recordTimer.stop();
			_recordTimer.removeEventListener(TimerEvent.TIMER,recordTimerTimer);
		}
		//
		private function recordTimerTimer(e:TimerEvent):void{
			var $bmd:BitmapData=new BitmapData(_target.width,_target.height,true);
			$bmd.draw(_target,null,null,null,bounds);
			var $data:String=Base64.encodeByteArray(BitmapDataEncoder.encodeToByteArray($bmd));
			
			if(_datas[_datas.length-1]!=$data)
				_datas.push($data);
			
			if(this.parent!=null)
				_simulateMovieClip.setFrames(datas);
		}
	}
}